package com.soo.hoo.admin.service.impl;

import com.soo.hoo.admin.component.DefaultWebSocketHandler;
import com.soo.hoo.admin.service.MessageService;
import com.soo.hoo.core.exception.GlobalException;
import com.soo.hoo.core.model.MessagePayload;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>消息服务</p>
 *
 * @author 杨燚
 * @since 2023/6/9
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MessageServiceImpl implements MessageService {

    private final DefaultWebSocketHandler webSocketHandler;

    @Override
    public <T> void sendMessage(MessagePayload<T> message) {
        try {
            webSocketHandler.sendMessage(message);
        } catch (Exception e) {
            // todo 保存发送异常信息
            throw new GlobalException("发送消息失败：" + e.getMessage());
        }
    }
}
